<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单聊天室</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #f5f5f5;
        }
        .chat-container {
            width: 80%;
            max-width: 600px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        .chat-header {
            background-color: #007bff;
            color: #fff;
            padding: 10px;
            text-align: center;
            font-size: 1.2em;
        }
        .chat-messages {
            height: 300px;
            overflow-y: auto;
            padding: 10px;
            border-bottom: 1px solid #ddd;
        }
        .chat-message {
            margin-bottom: 10px;
        }
        .chat-message .username {
            font-weight: bold;
            margin-right: 5px;
        }
        .chat-input {
            display: flex;
            padding: 10px;
        }
        .chat-input input[type="text"] {
            flex: 1;
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
        }
        .chat-input button {
            padding: 5px 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .chat-input button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">简单聊天室</div>
        <div class="chat-messages" id="chatMessages"></div>
        <div class="chat-input">
            <input type="text" id="username" placeholder="请输入用户名">
            <input type="text" id="message" placeholder="请输入消息">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>

    <script>
        const chatMessages = document.getElementById('chatMessages');
        const usernameInput = document.getElementById('username');
        const messageInput = document.getElementById('message');

        // 加载已存储的消息
        window.onload = function() {
            const storedMessages = JSON.parse(localStorage.getItem('messages')) || [];
            storedMessages.forEach(message => {
                displayMessage(message.username, message.text);
            });
        };

        function sendMessage() {
            const username = usernameInput.value.trim();
            const message = messageInput.value.trim();
            if (username && message) {
                const messageObj = { username, text: message };
                displayMessage(username, message);
                saveMessage(messageObj);
                messageInput.value = '';
            }
        }

        function displayMessage(username, message) {
            const messageElement = document.createElement('div');
            messageElement.classList.add('chat-message');
            messageElement.innerHTML = `<span class="username">${username}</span>: ${message}`;
            chatMessages.appendChild(messageElement);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        function saveMessage(message) {
            const messages = JSON.parse(localStorage.getItem('messages')) || [];
            messages.push(message);
            localStorage.setItem('messages', JSON.stringify(messages));
        }
    </script>
</body>
</html>